package com.linghang.lhonline.service.Impl;

import com.linghang.lhonline.dataobject.Users;
import com.linghang.lhonline.repository.UsersRepository;
import com.linghang.lhonline.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {


    private UsersRepository usersRepository;

    @Autowired
    public UsersServiceImpl(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

    @Override
    public Users save(Users users) {
        return usersRepository.save(users);
    }


    @Override
    public List<Users> findByGroupName(String groupName) {
        return usersRepository.findByGroupName(groupName);
    }

    @Override
    public void delete(String studentId) {
        Users users = usersRepository.findByStudentId(studentId);
        usersRepository.delete(users);
    }

    @Override
    public Users findByStudentId(String studentId) {
        return usersRepository.findByStudentId(studentId);
    }

    @Override
    public List<Users> findAll() {
        return usersRepository.findAll();
    }

    @Override
    public List<Users> crewFindByGroupName(String groupName) {

        List<Users> usersList = usersRepository.findByGroupName(groupName);
        List<Users> users = new ArrayList<>();

        for (Users user : usersList) {
            if (user.getPosition() == 0) {
                users.add(user);
            }
        }

        return users;
    }
}
